package Practice;
// 分割两个字符串得到回文串
public class PracticeSolution2 {
    public static void main(String[] args) {
        System.out.println(checkPalindromeFormation("pvhmupgqeltozftlmfjjde"
                ,"yjgpzbezspnnpszebzmhvp"));
    }

    public static boolean checkPalindromeFormation(String a, String b) {
        //如果a或b是回文串，则一定返回true
        if(isPalindrom(a)||isPalindrom(b))
            return true;
        int length = a.length()/2;
        boolean fromToBack = true,backToFront = true;
        for(int i = 0,j=a.length()-1 ; i < length ; i++,j--){
            if(!fromToBack && !backToFront)
                return false;
            if(fromToBack){
                if(a.charAt(i) != b.charAt(j)){
                    String strA = a.substring(i,j+1);
                    String strB = b.substring(i,j+1);
                    if(isPalindrom(strA)||isPalindrom(strB)){
                        return true;
                    }
                    fromToBack = false;
                }
            }
            if(backToFront){
                if(a.charAt(j) != b.charAt(i)){
                    String strA = a.substring(i,j+1);
                    String strB = b.substring(i,j+1);
                    if(isPalindrom(strA)||isPalindrom(strB)){
                        return true;
                    }
                    backToFront = false;
                }
            }
        }
        if(backToFront || fromToBack)
            return true;
        return false;
    }
    public static boolean isPalindrom(String str){
        for(int i = 0 , j = str.length()-1 ; i < str.length()/2 ; i++,j--){
            if(str.charAt(i) != str.charAt(j))
                return false;
        }
        return true;
    }
}
